import Vue from 'vue';
import Router from 'vue-router';
import login from "./components/login.vue";
import Home from './components/home.vue';
import Welcome from './components/welcome.vue';
import User from './components/user/users.vue';
import Right from './components/right/rights.vue';
import Role from "./components/right/role.vue";
import CateGories from "./components/goods/categorie.vue";
import GoodParams from './components/goods/goodparams.vue'
import Goods from './components/goods/goods.vue'
import GoodAdd from './components/goods/goodadd'
import Order from './components/order/order.vue'
import Report from './components/report/report.vue'
Vue.use(Router);

const router = new Router({
        routes: [
            { path: '/', redirect: '/login' },
            {
                path: '/login',
                component: login
            },
            {
                path: '/home',
                redirect: '/welcome',
                component: Home,
                children: [{
                        path: '/welcome',
                        component: Welcome
                    }, {
                        path: '/users',
                        component: User
                    },
                    {
                        path: '/rights',
                        component: Right
                    },
                    {
                        path: '/roles',
                        component: Role
                    },
                    {
                        path: '/categories',
                        component: CateGories,
                    },
                    {
                        path: '/params',
                        component: GoodParams,
                    },
                    {
                        path: '/goods',
                        component: Goods,
                    },
                    {
                        path: '/goods/add',
                        component: GoodAdd,
                    },
                    {
                        path: '/orders',
                        component: Order,
                    },
                    {
                        path: '/reports',
                        component: Report,
                    },
                ]
            }
        ]
    })
    // 路由守卫 识别登录有没有携带token值
router.beforeEach((to, from, next) => {
    if (to.path == '/login') {
        next()
    } else {
        if (sessionStorage.getItem('token')) {
            next()
        } else {
            next('/login')
        }
    }
})

export default router